package org.dong.control;


import org.dong.pojo.AdminUser;
import org.dong.pojo.Dto;
import org.dong.service.DoctorService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AdminUserControl {

    @Resource
    DoctorService doctorService;

    @PostMapping( "/AdminUser/login")
    public Dto AdminLogin(HttpServletRequest req, HttpServletResponse resp) {
        Dto dto = new Dto();

        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String code = req.getParameter("code");


        if (code.equalsIgnoreCase((String) req.getSession().getAttribute("codeStr"))) {
             AdminUser adminUser = doctorService.DoctorLogin(account, password);
            if (adminUser != null) {
                req.getSession().setAttribute("user",adminUser);
                Map<String, Object> datas = new HashMap<String, Object>();
                datas.put("user", adminUser);
                dto.setDatas(datas);
                if(adminUser.getType() == 2){
                    dto.setLocation("main");
                }else{
                    dto.setLocation("report_list");
                }
                dto.setErrMsg("登录成功");
                dto.setStatusCode(200);

            } else {
                dto.setErrMsg("登录失败");
                dto.setStatusCode(2001);
            }
        } else {
            dto.setErrMsg("验证码错误");
            dto.setStatusCode(2001);
        }


        return dto;

    }
}

